package com.vhly.aapt;

import com.vhly.aapt.io.OrderInputStream;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-4-26
 * Time: 16:07:03
 */
public class ResXMLTree_attribute extends ResXMLTree_node {
    public int nsStringPoolIndex;
    public int nameStringPoolIndex;
    public int rawStringPoolIndex;
    public ResValue value;

    @Override
    public String toXML() {
        String ret = "";
        StringBuffer sb = new StringBuffer();
        if(nsStringPoolIndex > -1){
            String nss = pool.entries.get(nsStringPoolIndex);
            int index = nss.lastIndexOf("/");
            if(index != -1){
                nss = nss.substring(index+1);
            }
            sb.append(nss).append(':');
        }
        if(nameStringPoolIndex > -1){
            sb.append(pool.entries.get(nameStringPoolIndex));
        }
        sb.append('=');
        if(rawStringPoolIndex > -1){
            sb.append('"').append(pool.entries.get(rawStringPoolIndex)).append('"');
        }else if(value != null){
            sb.append('"').append(value.toXML()).append('"');
        }
        ret = sb.toString();
        sb = null;
        return ret;
    }

    @Override
    public void read(OrderInputStream din) throws IOException {
        nsStringPoolIndex = din.readInt();
        nameStringPoolIndex = din.readInt();
        rawStringPoolIndex = din.readInt();
        value = new ResValue();
        value.read(din);
        value.pool = pool;
    }
}